<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="合作商名称" prop="partnersName">
        <el-input
          v-model="queryParams.partnersName"
          placeholder="请输入合作商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['partners:partners:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['partners:partners:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['partners:partners:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partnersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合作商id" align="center" prop="partnersId" />
      <el-table-column label="合作商名称" align="center" prop="partnersName" width="120"/>
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" width="115"/>
      <el-table-column label="所属区域" align="center" width="160">
        <template slot-scope="scope">
          <div>{{ scope.row.provinceName }}/{{ scope.row.cityName }}/{{ scope.row.countyName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="address" width="180"/>
      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="partnersAttribute" align="center" label="属性" width="">
        <template slot-scope="scope">
          <div class="vertical-content">
            <div v-if="scope.row.partnersAttribute === 1">个人</div>
            <div v-if="scope.row.partnersAttribute === 2">企业</div>
            <div v-if="scope.row.partnersAttribute === 3">其他</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="行业类别" align="center" prop="partnersCategoryName" />
      <el-table-column label="备注" align="center" prop="remark" width="150"/>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['partners:partners:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['partners:partners:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合作商管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="合作商名称" prop="partnersName">
          <el-input v-model="form.partnersName" placeholder="请输入合作商名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所属区域" prop="belongingArea">
          <el-cascader
            placeholder="所属区域"
            v-model="form.belongingArea"
            :options="chooseAddressData"
            :props="{ value: 'region_sn', label: 'region_name' }"
            filterable></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="合作商属性" prop="partnersAttribute">
          <el-radio-group v-model="form.partnersAttribute">
            <el-radio
              v-for="item in partnersAttributeOptions"
              :key="item.value"
              :label="item.value"
            >{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="行业类别" prop="partnersCategoryId">
          <el-select v-model="form.partnersCategoryId" filterable placeholder="请选择">
            <el-option
              v-for="item in partnersCategoryOptions"
              :key="item.partnersCategoryId"
              :label="item.partnersCategoryName"
              :value="item.partnersCategoryId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPartners, getPartners, delPartners, addPartners, updatePartners, chooseAddressList, changePartnersStatus } from "@/api/partners/partners";
import { listCategoryNotPage } from '@/api/partners/category'
import { changeUserStatus } from '@/api/system/user'

export default {
  name: "Partners",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合作商管理表格数据
      partnersList: [],
      partnersCategoryOptions:[],
      chooseAddressData:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        partnersName: null,
        contact: null,
        contactPhone: null,
        province: null,
        city: null,
        county: null,
        address: null,
        status: null,
        partnersAttribute: null,
        partnersCategoryId: null
      },
      partnersAttributeOptions: [
        { value: 1, label: '个人' },
        { value: 2, label: '企业' },
        { value: 3, label: '其他' }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        partnersName: [
          { required: true, message: "合作商名称不能为空", trigger: "blur" }
        ],
        contact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        belongingArea:[
          { required: true, message: "所属区域不能为空", trigger: "blur" }
        ],
        address:[
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        partnersAttribute: [
          { required: true, message: "合作商属性不能为空", trigger: "blur" }
        ],
        partnersCategoryId: [
          { required: true, message: "行业类别不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.chooseAddress();
    this.partnersCategoryList();
  },
  methods: {
    chooseAddress(){
      chooseAddressList().then(response => {
        this.chooseAddressData = response.data;
        console.log(response.data);
      });
    },
    partnersCategoryList(){
      listCategoryNotPage().then(response => {
        this.partnersCategoryOptions = response.data;
      });
    },
    /** 查询合作商管理列表 */
    getList() {
      this.loading = true;
      listPartners(this.queryParams).then(response => {
        this.partnersList = response.data.data;
        this.total = response.data.total??0;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partnersId: null,
        partnersName: null,
        contact: null,
        contactPhone: null,
        province: null,
        city: null,
        county: null,
        address: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        status: '0',
        partnersAttribute: 1,
        partnersCategoryId: null,
        belongingArea:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partnersId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const partnersId = row.partnersId || this.ids
      getPartners(partnersId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合作商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partnersId != null) {
            updatePartners(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPartners(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partnersIds = row.partnersId || this.ids;
      this.$modal.confirm('是否确认删除合作商编号为"' + partnersIds + '"的数据项？').then(function() {
        return delPartners(partnersIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/partners/export', {
        ...this.queryParams
      }, `partners_${new Date().getTime()}.xlsx`)
    },
    // 用户状态修改
    handleStatusChange(row) {
      console.log(row)
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.partnersName + '"合作商吗？').then(function() {
        return changePartnersStatus(row.partnersId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
  }
};
</script>
